package com.kalaqia.gradle.jiagu;

import org.gradle.api.DefaultTask;
import org.gradle.api.tasks.TaskAction;

/**
 * function:the task of download 360 jiagu package into local dir
 * project:AutoBuildGradlePlugin
 * describe:
 * created owner:kalaqia 421407393@qq.com
 * created date:2018/1/3 10:41.
 */
class DownloadJiaGu360Task extends DefaultTask {

    @SuppressWarnings("GroovyUnusedDeclaration")
    @TaskAction
    run() {

        //create download bat script
        def f = new File("${project.rootDir}/downloadJiaGu360.bat");
        if (!f.exists()) {
            def out = f.newPrintWriter()
            out.write("${DownloadScript.bat()}");
            out.flush()
            out.close()
        }

        //create dirs
        def jiaGuScript = new File("${project.rootDir}/360JiaGu/JiaGu.bat");
        if (!jiaGuScript.getParentFile().exists()) {
            jiaGuScript.getParentFile().mkdir();
        }

        //create 360 jiaGu bat script
        if (!jiaGuScript.exists()) {
            def out = jiaGuScript.newPrintWriter()
            out.write("${JiaGu360Script.bat()}");
            out.flush()
            out.close()
        }

        def channels = new File("${project.rootDir}/360JiaGu/channels.txt");

        if (!channels.exists()) {
            channels.createNewFile();
        }

        project.exec {
            commandLine "${project.rootDir}/downloadJiaGu360.bat", "${project.rootDir}"
        }

    }

}
